﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Project.MODEL;
using Project.DAL.System;
using System.Dynamic;
using Newtonsoft.Json;

namespace Project.BLL.System
{
    public class OrganizationBll
    {
        OrganizationDal dal = new OrganizationDal();

        #region Department 部门相关操作

        /// <summary>
        /// 获取所有部门列表
        /// </summary>
        /// <returns></returns>
        public string GetDepartmentAll()
        {
            var datas = dal.GetDepartment();

            var list = from d in datas
                       select new
                       {
                           d.deptID,
                           d.deptName,
                           d.remark
                       };

            dynamic data = new ExpandoObject();
            data.code = 1;
            data.msg = "部门列表";
            data.count = list.Count();
            data.data = list.ToList();

            return JsonConvert.SerializeObject(data);
        }

        /// 添加部门
        /// </summary>
        /// <param name="dept"></param>
        /// <returns></returns>
        public string AddDepartment(Department dept)
        {
            //统一定义返回数据格式
            dynamic data = new ExpandoObject();
            int num = dal.AddDepartment(dept);
            data.code = num;
            data.msg = num > 0 ? "添加成功！" : "添加失败！";
            return JsonConvert.SerializeObject(data);
        }

        /// <summary>
        /// 修改部门
        /// </summary>
        /// <param name="dept"></param>
        /// <returns></returns>
        public string UpdateDepartment(Department dept)
        {

            int num = dal.UpdateDepartment(dept);

            //统一定义返回数据格式
            dynamic data = new ExpandoObject();
            data.code = num;
            data.msg = num > 0 ? "修改成功！" : "修改失败！";

            return JsonConvert.SerializeObject(data);
        }

        /// <summary>
        /// 删除部门
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public string DeleteDepartmentByID(int id)
        {
            var datas = dal.GetDepartment();
            var dept = datas.Where(d => d.deptID == id).FirstOrDefault();
            //统一定义返回数据格式
            dynamic data = new ExpandoObject();
            if (dept.Roles.Count > 0)
            {
                data.code = -1;
                data.msg = "该部门正在使用中无法删除！";
            }
            else
            {
                int num = dal.DeleteDepartment(dept);
                data.code = num;
                data.msg = num > 0 ? "删除成功！" : "删除失败！";
            }
            return JsonConvert.SerializeObject(data);
        }

        #endregion

        #region Roles 角色相关操作

        /// <summary>
        /// 获取所有角色列表
        /// </summary>
        /// <returns></returns>
        public string GetRolesAll()
        {
            var datas = dal.GetRoles();

            var list = from d in datas
                       select new
                       {
                           d.roleID,
                           d.deptID,
                           d.roleName,
                           d.remark,
                           d.type
                       };

            dynamic data = new ExpandoObject();
            data.code = 1;
            data.msg = "角色列表";
            data.count = list.Count();
            data.data = list.ToList();

            return JsonConvert.SerializeObject(data);
        }

        /// 添加角色
        /// </summary>
        /// <param name="role"></param>
        /// <returns></returns>
        public string AddRoles(Roles role)
        {
            //统一定义返回数据格式
            dynamic data = new ExpandoObject();
            int num = dal.AddRoles(role);
            data.code = num;
            data.msg = num > 0 ? "添加成功！" : "添加失败！";
            return JsonConvert.SerializeObject(data);
        }

        /// <summary>
        /// 修改角色
        /// </summary>
        /// <param name="role"></param>
        /// <returns></returns>
        public string UpdateRoles(Roles role)
        {

            int num = dal.UpdateRoles(role);

            //统一定义返回数据格式
            dynamic data = new ExpandoObject();
            data.code = num;
            data.msg = num > 0 ? "修改成功！" : "修改失败！";

            return JsonConvert.SerializeObject(data);
        }

        /// <summary>
        /// 删除角色
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public string DeleteRolesByID(int id)
        {
            var datas = dal.GetRoles();
            var role = datas.Where(d => d.roleID == id).FirstOrDefault();
            //统一定义返回数据格式
            dynamic data = new ExpandoObject();
            if (role.Authority.Count > 0)
            {
                data.code = -1;
                data.msg = "该角色正在使用中无法删除！";
            }
            else
            {
                int num = dal.DeleteRoles(role);
                data.code = num;
                data.msg = num > 0 ? "删除成功！" : "删除失败！";
            }
            return JsonConvert.SerializeObject(data);
        }

        #endregion

        /// <summary>
        /// 获取组织结构数据（部门+角色）
        /// </summary>
        /// <returns></returns>
        public string GetOrganization() {
            var datas = dal.GetDepartment();

            dynamic data = new ExpandoObject();

            List<dynamic> list = new List<dynamic>();

            foreach (var d in datas)
            {
                dynamic dept = new ExpandoObject();
                dept.deptID = d.deptID;
                dept.deptName = d.deptName;
                dept.type = d.type;
                dept.remark = d.remark;

                List<dynamic> roles = new List<dynamic>();
                foreach (var r in d.Roles)
                {
                    dynamic role = new ExpandoObject();
                    role.roleID = r.roleID;
                    role.deptID = r.deptID;
                    role.deptName = r.Department.deptName;
                    role.roleName = r.roleName;
                    role.type = r.type;
                    role.remark = r.remark;
                    roles.Add(role);
                }
                dept.roles = roles;

                list.Add(dept);
            }

            data.code = 1;
            data.msg = "组织列表";
            data.count = list.Count();
            data.data = list;

            return JsonConvert.SerializeObject(data);
        }


        
    }
}
